package com.aos.erp.schools.rest.schoolclass;


import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.model.schoolclass.*;
import com.aos.erp.schools.service.common.DictService;
import com.aos.erp.schools.service.schoolclass.DriversSchoolClassService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;


@Slf4j
@RestController("DriversSchoolClassController")
@RequestMapping("/api/schoolclass")
@Api(tags = {"班型管理"})
public class DriversSchoolClassController {

    @Resource
    private DriversSchoolClassService driversSchoolClassService;

    @Resource
    private DictService dictService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询")
    public ResultEntity<PageBean<DriversSchoolClassInfoVo>> queryPageBean(QueryClassRo queryClassRo) throws Exception {
        return ResultEntity.success(driversSchoolClassService.queryByPage(queryClassRo));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增班型")
    public ResultEntity addCoach(@RequestBody @Validated AddDriversSchoolClassRo addDriversSchoolClassRo, BindingResult bindingResult,String currentUserId) throws Exception {
        //数据 验证信息

        if (bindingResult.hasErrors()) {
            return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        driversSchoolClassService.addBean(addDriversSchoolClassRo,currentUserId);
        return ResultEntity.success();
    }


    @ApiOperation(value = "删除班型", notes = "删除班型")
    @GetMapping(value = "/delete")
    public ResultEntity deleteClass(@RequestParam  @NotNull(message = "参数不能为空") String ids) throws BizException {
        driversSchoolClassService.deleteClass(ids);
        return ResultEntity.success();
//        return driversSchoolClassService.deleteClass(ids)>=1?ResultEntity.success():ResultEntity.failed();
    }


    /**
     * 修改班型
     * @[param UpdateDivisionRos  修改班型
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改班型", notes = "修改班型")
    public ResultEntity updateClass( @RequestBody @Validated UpdateDriversSchoolClassRo updateDriversSchoolClassRo, BindingResult bindingResult){
        //数据 验证信息
        if (bindingResult.hasErrors()) {
            return  ResultEntity.failed(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        driversSchoolClassService.updateClass(updateDriversSchoolClassRo);
        return ResultEntity.success();
    }

    /**
     * 班型详情
     * @[param id  班型id
     * @return
     */
    @ApiOperation(value = "班型详情", notes = "班型详情")
    @GetMapping("/info")
    public ResultEntity<DriversSchoolClassInfoVo> classInfo(@RequestParam Long id) throws BizException {
        if(id==null){
            return ResultEntity.failed("参数不能为空");
        }
        return ResultEntity.success(driversSchoolClassService.classInfo(id));
    }


    /**
     * 页面基础数据获取
     * @[param id  班型id
     * @return
     */
    @ApiOperation(value = "页面基础数据", notes = "页面基础数据")
    @GetMapping("/getBaseData")
    public ResultEntity<DriversSchoolClassBaseDataVo> initBaseData() throws BizException {

        DriversSchoolClassBaseDataVo baseDataVo = new DriversSchoolClassBaseDataVo();

        baseDataVo.setClassTypeList(dictService.findByParams("classType"));
        baseDataVo.setCarTypeList(dictService.findByParams("car"));
        baseDataVo.setLearnTimeList(dictService.findByParams("learnTime"));

        return ResultEntity.success(baseDataVo);
    }

}